﻿Type=Activity
Version=1.90
FullScreen=False
IncludeTitle=True
@EndOfDesignText@
'Activity module
Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'These variables can be accessed from all modules.
	Dim send_what As String
End Sub

Sub Globals
	'These global variables will be redeclared each time the activity is created.
	'These variables can only be accessed from this module.
	Dim yawdir As ToggleButton
	Dim cli_connected As Boolean
	Dim labels As Label
	Dim timer2 As Timer
	timer2.Initialize("Timer2", 200)
	Dim checkboxes(10) As CheckBox
	Dim features(10) As String
	features= Array As String ("PPM", "VBAT", "INFLIGHT_ACC_CAL","SPEKTRUM", "MOTOR_STOP","SERVO_TILT","CAMTRIG","GYRO_SMOOTHING","LED_RING","GPS")
	Dim Set_Features As Button
	Dim vsv As Panel 'ScrollView
	Dim lbl2 As Label
	Dim sk1 As SeekBar
	Dim cd As CustomDialog
	Dim valor As Int
	Dim sets(50) As Int 
	Dim cuantos As Int
	Dim set_label As List
	'Dim settings(8) As Label 'deadband=0, yawdeadband=1,
	Dim rcsettings As Button
	Dim changed(50) As Boolean 'deadband=0, yawdeadband=1,
	Dim labeltext(50) As Label
	Dim labelvalue(50) As Label
	Dim Maxs(33) As Int
	Dim Mins(33) As Int
	Maxs= Array As Int (32,32,1700,2000,2000,2000,2000,2000,498,498,1,200,50,50,   1,2000,2000,2000,2000,2000,255, 100, 100,3000,3000,3000,3000,3000,3000,250,256,115200,115200)
	Mins= Array As Int ( 0, 0,1200,   0,   0,   0,   0,   0, 50, 50,0, 10,10,10,  -1,   0,   0,   0,   0,   0,  0,-100,-100, 100, 100, 100, 100, 100, 100,  0,  0,  1200,  1200)
	Dim valor_minimo As Int
	Dim valor_maximo As Int
	Dim vbat As Button
	Dim read_again As Button
'	 { "deadband", VAR_UINT8, &cfg.deadband,                     0, 32 }, 0
'    { "yawdeadband", VAR_UINT8, &cfg.yawdeadband,				 0, 100 },
'    { "midrc", VAR_UINT16, &cfg.midrc,							 1200, 1700 },
'    { "minthrottle", VAR_UINT16, &cfg.minthrottle,				 0, 2000 },
'    { "maxthrottle", VAR_UINT16, &cfg.maxthrottle,				 0, 2000 },			Rc settings
'    { "mincommand", VAR_UINT16, &cfg.mincommand,				 0, 2000 },
'    { "mincheck", VAR_UINT16, &cfg.mincheck,					 0, 2000 },
'    { "maxcheck", VAR_UINT16, &cfg.maxcheck,					 0, 2000 }, 7 

'    { "motor_pwm_rate", VAR_UINT16, &cfg.motor_pwm_rate,		50, 498 }, 8
'    { "servo_pwm_rate", VAR_UINT16, &cfg.servo_pwm_rate, 		50, 498 }, 9
'    { "spektrum_hires", VAR_UINT8, &cfg.spektrum_hires, 		 0, 1 }, 10

'    { "vbatscale", VAR_UINT8, &cfg.vbatscale,					10, 200 },11
'    { "vbatmaxcellvoltage", VAR_UINT8, &cfg.vbatmaxcellvoltage,10, 50 }, 			batt settings
'    { "vbatmincellvoltage", VAR_UINT8, &cfg.vbatmincellvoltage,10, 50 },13

'    { "yaw_direction", VAR_INT8, &cfg.yaw_direction, 			-1, 1 }, 14
'    { "wing_left_mid", VAR_UINT16, &cfg.wing_left_mid, 		 0, 2000 }, 15
'    { "wing_right_mid", VAR_UINT16, &cfg.wing_right_mid,		 0, 2000 },
'    { "tri_yaw_middle", VAR_UINT16, &cfg.tri_yaw_middle,		 0, 2000 },        tri/wing settings
'    { "tri_yaw_min", VAR_UINT16, &cfg.tri_yaw_min,				 0, 2000 },
'    { "tri_yaw_max", VAR_UINT16, &cfg.tri_yaw_max,				 0, 2000 }, 19

'    { "gimbal_flags", VAR_UINT8, &cfg.gimbal_flags,			 0, 255}, 20
'    { "gimbal_pitch_gain", VAR_INT8, &cfg.gimbal_pitch_gain, -100, 100 },
'    { "gimbal_roll_gain", VAR_INT8, &cfg.gimbal_roll_gain,	  -100, 100 },
'    { "gimbal_pitch_min", VAR_UINT16, &cfg.gimbal_pitch_min,  100, 3000 },
'    { "gimbal_pitch_max", VAR_UINT16, &cfg.gimbal_pitch_max,  100, 3000 },    	gimbal settings
'    { "gimbal_pitch_mid", VAR_UINT16, &cfg.gimbal_pitch_mid,  100, 3000 },
'    { "gimbal_roll_min", VAR_UINT16, &cfg.gimbal_roll_min,    100, 3000 },
'    { "gimbal_roll_max", VAR_UINT16, &cfg.gimbal_roll_max,    100, 3000 },
'    { "gimbal_roll_mid", VAR_UINT16, &cfg.gimbal_roll_mid,    100, 3000 }, 28 

 
'    { "acc_lpf_factor", VAR_UINT8, &cfg.acc_lpf_factor,		 0, 250 },  29
'    { "gyro_lpf", VAR_UINT16, &cfg.gyro_lpf,					 0, 256 },  30

'    { "gps_baudrate", VAR_UINT32, &cfg.gps_baudrate,		  1200, 115200 }, 31 
'    { "serial_baudrate", VAR_UINT32, &cfg.serial_baudrate,	  1200, 115200 }, 32
 
'deadband = 0						0	
'yawdeadband = 0
'midrc = 1500
'minthrottle = 1150
'maxthrottle = 1850
'mincommand = 1000
'mincheck = 1100
'maxcheck = 1900					7

'motor_pwm_rate = 400				8	
'servo_pwm_rate = 50
'spektrum_hires = 0					10

'vbatscale = 110					11
'vbatmaxcellvoltage = 43
'vbatmincellvoltage = 33			13

'yaw_direction = 1					14

'wing_left_mid = 1500				15
'wing_right_mid = 1500
'tri_yaw_middle = 1500
'tri_yaw_min = 1020
'tri_yaw_max = 2000					19

'gimbal_flags = 1					20
'gimbal_pitch_gain = 10
'gimbal_roll_gain = 10
'gimbal_pitch_min = 1020
'gimbal_pitch_max = 2000
'gimbal_pitch_mid = 1500
'gimbal_roll_min = 1020
'gimbal_roll_max = 2000
'gimbal_roll_mid = 1500				28			

'acc_lpf_factor = 4					29
'gyro_lpf = 42						30

'gps_baudrate = 9600				31
'serial_baudrate = 115200			32



	Dim panel_rc_settings As Panel
	Dim panel_vbat As Panel
	Dim panel_tri As Panel
	Dim panel_gimbal As Panel
	Dim panel_misc As Panel
	Dim panel_baud As Panel
	
	Dim gimbal As Button
	Dim tri As Button
	Dim Misc As Button
	Dim Baud As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
	Activity.AddMenuItem("Close Cli","close")
	Activity.AddMenuItem("Save Data","save")
	Activity.AddMenuItem("Read Data","read")
	cuantos=0
	set_label.Initialize
	set_label.AddAll( Array As String ("Deadband","yaw Deadband","mid RC", "min Throttle", "max Throttle", "min Command","min Check","max Check","motor_pwm_rate", "servo_pwm_rate","spektrum_hires", "vbat scale", "vbat max cell voltage","vbat min cell voltage", "yaw_direction","wing_left_mid","wing_right_mid","tri_yaw_middle","tri_yaw_min","tri_yaw_max","gimbal_flags","gimbal_pitch_gain","gimbal_roll_gain","gimbal_pitch_min","gimbal_pitch_max","gimbal_pitch_mid","gimbal_roll_min","gimbal_roll_max","gimbal_roll_mid", "acc_lpf_factor", "gps_baudrate","serial_baudrate"))
	
	For i=0 To changed.Length-1
		changed(i)=False
	Next
	'vsv.Initialize(100%y)
	vsv.Initialize("")
	labels.Initialize("")
	For i=0 To 9
	If i<5 Then 
		labels.Initialize("")
		vsv.AddView(labels,5%x,5%y + i*(10%y),30%x,10%y)
		labels.Text=features(i)
		labels.Left=5%x
		labels.Gravity=Bit.Or(Gravity.LEFT,Gravity.CENTER_VERTICAL)
		checkboxes(i).Initialize("")
		vsv.AddView(checkboxes(i),labels.width + 10%x , 5%y + i*(10%y) ,50dip, 10%y)
	Else 
		labels.Initialize("")
		vsv.AddView(labels,70%x,5%y + (i-5)*(10%y),30%x,10%y)
		labels.Text=features(i)
		'labels.left=65%x
		labels.Gravity=Bit.Or(Gravity.LEFT,Gravity.CENTER_VERTICAL)
		checkboxes(i).Initialize("")
		vsv.AddView(checkboxes(i),55%x , 5%y + (i-5)*(10%y) ,50dip, 10%y)
	End If 
	Next
	
	rcsettings.Initialize("rcsettings")
	Activity.AddView(rcsettings,0%x,60%y,30%x,50dip)
	rcsettings.Text="RC settings"
	rcsettings.Enabled=True
	
	vbat.Initialize("vbat")
	Activity.AddView(vbat,35%x,60%y,30%x,50dip)
	vbat.Text="Batterie Settings"
	vbat.Enabled=True
	
	tri.Initialize("Tri")
	Activity.AddView(tri,70%x,60%y,30%x,50dip)
	tri.Text="Tri/Wing Settings"
	tri.Enabled=True
	
	gimbal.Initialize("gimbal")
	Activity.AddView(gimbal,0%x,75%y,30%x,50dip)
	gimbal.Text="Gimbal Settings"
	gimbal.Enabled=True
	
	Misc.Initialize("misc")
	Activity.AddView(Misc,35%x,75%y,30%x,50dip)
	Misc.Text="Misc Settings"
	Misc.Enabled=True
	
	Baud.Initialize("baud")
	Activity.AddView(Baud,70%x,75%y,30%x,50dip)
	Baud.Text="Baud Settings"
	Baud.Enabled=True
	
	
	Activity.AddView(vsv,0%x,0%y,100%x,90%y)
	Set_Features.Initialize("Set_Features")
	Activity.AddView(Set_Features, 0%x,90%y,20%x,10%y)
	Set_Features.Text="Set"
	Set_Features.Left=Activity.Width/2-Set_Features.Width/2
	
	read_again.Initialize("read_again")
	Activity.AddView(read_again, 0%x,90%y,20%x,10%y)
	read_again.Text="Read"
	read_again.Left=100%x-read_again.width
	
	
	data
	
	For i=0 To labelvalue.Length-1
		labelvalue(i).Initialize("")
		labeltext(i).Initialize("")
	Next 
End Sub
Sub save_click
	Dim fi As String
	Dim fd As FileDialog
	Dim ret As Int
	'fi = fd.ChosenName
	File.MakeDir("/mnt/sdcard","nazecontrol")
	fd.FilePath="/mnt/sdcard/nazecontrol"
	fd.FileFilter=".snaz"
	If File.Exists(fd.FilePath, fi) = True Then  
	   ' Show list of files. User can select one or click one of the buttons shown: 
	   	ret = fd.Show("Save" & fi, "Save","","Don't save", Null)
	   	If ret = -2 Then 
			Return ' "Don't save" was pressed.
	   	Else 
			Dim temp As String
			temp=DateTime.time(DateTime.Now) & "_" & DateTime.Date(DateTime.Now)
			temp=temp.Replace("/", "-")
			temp=temp.Replace(":","")
			fi = fd.ChosenName & " " & temp
		   	If fi.EndsWith("snaz") Then 
		   	Else 
		   		fi=fi & ".snaz"
			End If
			Dim TW As TextWriter
			TW.Initialize(File.OpenOutput(fd.FilePath, fi, False))
			Dim writelist As List
			writelist.Initialize
			For i= 0 To 9
				If checkboxes(i).Checked=True Then
					writelist.Add(1)
				Else 
					writelist.Add(0)
				End If 
			Next 
			writelist.Addall(sets)
			TW.writelist(writelist)
			TW.Close
		End If
	End If 
	
End Sub
Sub read_click
	Dim fi As String
	Dim fd As FileDialog
	Dim ret As Int
	'fi = fd.ChosenName
	fd.FileFilter=".snaz"
	File.MakeDir("/mnt/sdcard","nazecontrol")
	fd.FilePath="/mnt/sdcard/nazecontrol"
	If File.Exists(fd.FilePath, fi) = True Then  
	   ' Show list of files. User can select one or click one of the buttons shown: 
	   	ret = fd.Show("Read" & fi, "Read","","Cancel", Null)
	   	If ret = -2 Then 
			Return ' "Don't save" was pressed.
	   	Else
			Dim TR As TextReader
			TR.Initialize(File.OpenInput(fd.FilePath, fd.ChosenName))
			Dim listdata As List
			listdata.Initialize
			listdata=TR.ReadList
		End If 
	End If 
	For i=0 To 9
		If listdata.Get(i)="1" Then 
			checkboxes(i).Checked=True
		Else 
			checkboxes(i).Checked=False
		End If 	
	Next
	For i=0 To sets.Length-1
		sets(i)=listdata.Get(i+10)
	Next
	

End Sub

Sub read_again_Click
	data
End Sub
Sub rcsettings_Click
	deshabilitar
	
	panel_rc_settings.Initialize("")
	Activity.addview(panel_rc_settings,0%x, 0%y, 100%x, 100%y)
	Dim titulo As Label
	titulo.Initialize("")
	titulo.text= "RC settings"
	titulo.TextSize=18
	titulo.gravity=Gravity.CENTER
	panel_rc_settings.AddView(titulo, 50%x,0%y, 100%x,10%y)
	titulo.Left=50%x-titulo.Width/2
	panel_rc_settings.Color=Colors.ARGB(255,0,0,0)
	For n =0 To  3
		i=2*n
		labeltext(i).Initialize("")
		labelvalue(i).Initialize("labelvalue")
		labeltext(i).Text=set_label.Get(i)
		labelvalue(i).Text=sets(i)
		labelvalue(i).Tag=set_label.get(i)
		labelvalue(i).Color=Colors.White
		labelvalue(i).TextColor=Colors.Black
		panel_rc_settings.AddView(labeltext(i),5%x, 20%y+n*10%y,80dip,40dip)
		panel_rc_settings.AddView(labelvalue(i),5%x+labeltext(i).Width, 20%y+n*10%y,50dip,30dip)
		labelvalue(i).Gravity=Gravity.CENTER
		labeltext(i).Gravity=Gravity.CENTER
		
		labeltext(i+1).Initialize("")
		labelvalue(i+1).Initialize("labelvalue")
		labeltext(i+1).Text=set_label.Get(i+1)
		labelvalue(i+1).Text=sets(i+1)
		labelvalue(i+1).Tag=set_label.get(i+1)
		labelvalue(i+1).Color=Colors.White
		labelvalue(i+1).TextColor=Colors.Black
		panel_rc_settings.AddView(labeltext(i+1),55%x, 20%y+n*10%y,80dip,40dip)
		panel_rc_settings.AddView(labelvalue(i+1),55%x+labeltext(i+1).Width, 20%y+n*10%y,50dip,30dip)
		labelvalue(i+1).Gravity=Gravity.CENTER
		labeltext(i+1).Gravity=Gravity.CENTER
	Next
	Dim cerrar As Button
	cerrar.Initialize("cerrar")
	cerrar.Text="Close"
	panel_rc_settings.AddView(cerrar,50%x,90%y,100dip,40dip)
	cerrar.Left=50%x-cerrar.Width/2
	cerrar.Gravity=Gravity.CENTER
End Sub
Sub vbat_Click()
	deshabilitar
	
	panel_vbat.Initialize("")
	Activity.addview(panel_vbat,0%x, 0%y, 100%x, 100%y)
	Dim titulo As Label
	titulo.Initialize("")
	titulo.text= "Battery Settings"
	titulo.TextSize=18
	titulo.gravity=Gravity.CENTER
	panel_vbat.AddView(titulo, 50%x,0%y, 100%x,10%y)
	titulo.Left=50%x-titulo.Width/2
	panel_vbat.Color=Colors.ARGB(255,0,0,0)
	For i=0 To 2
		labeltext(i).Initialize("")
		labelvalue(i+11).Initialize("labelvalue")
		labeltext(i).Text=set_label.Get(i+11)
		labelvalue(i+11).Text=sets(i+11)
		labelvalue(i+11).Tag=set_label.get(i+11)
		labelvalue(i+11).Color=Colors.White
		labelvalue(i+11).TextColor=Colors.Black
		panel_vbat.AddView(labeltext(i),5%x, 20%y+i*10%y,50%x,40dip)
		panel_vbat.AddView(labelvalue(i+11),10%x+labeltext(i).Width, 20%y+i*10%y,50dip,30dip)
		labelvalue(i+11).Gravity=Gravity.CENTER
		labeltext(i).Gravity=Gravity.CENTER
	Next 
	Dim cerrar2 As Button
	cerrar2.Initialize("cerrar2")
	cerrar2.Text="Close"
	panel_vbat.AddView(cerrar2,50%x,90%y,100dip,40dip)
	cerrar2.Left=50%x-cerrar2.Width/2
	cerrar2.Gravity=Gravity.CENTER
	
End Sub

Sub tri_Click()
	deshabilitar
	
	panel_tri.Initialize("")
	Activity.addview(panel_tri,0%x, 0%y, 100%x, 100%y)
	Dim titulo As Label
	titulo.Initialize("")
	titulo.text= "Tri/Wing Settings"
	titulo.TextSize=18
	titulo.gravity=Gravity.CENTER
	panel_tri.AddView(titulo, 50%x,0%y, 100%x,10%y)
	titulo.Left=50%x-titulo.Width/2
	panel_tri.Color=Colors.ARGB(255,0,0,0)
	For i=0 To 5
		labeltext(i).Initialize("")
		labeltext(i).Text=set_label.Get(i+14)
		panel_tri.AddView(labeltext(i),5%x, 20%y+i*10%y,50%x,40dip)
		
		If i<>0 Then 
			labelvalue(i+14).Initialize("labelvalue")
			labelvalue(i+14).Text=sets(i+14)
			labelvalue(i+14).Tag=set_label.get(i+14)
			labelvalue(i+14).Color=Colors.White
			labelvalue(i+14).TextColor=Colors.Black
			labelvalue(i+14).Gravity=Gravity.CENTER
			panel_tri.AddView(labelvalue(i+14),10%x+labeltext(i).Width, 20%y+i*10%y,50dip,30dip)
		
		Else 
			yawdir.Initialize("direccion")
			yawdir.TextOff="-1"
			yawdir.TextOn="1"
			If sets(14)=1 Then 
				yawdir.Checked=True
			Else 
				yawdir.Checked=False
			End If 
			panel_tri.AddView(yawdir,10%x+labeltext(i).Width, 20%y-20dip,50dip,50dip)
			
		End If 
		
		
		
		labeltext(i).Gravity=Gravity.CENTER
	Next 
	Dim cerrar3 As Button
	cerrar3.Initialize("cerrar3")
	cerrar3.Text="Close"
	panel_tri.AddView(cerrar3,50%x,90%y,100dip,40dip)
	cerrar3.Left=50%x-cerrar3.Width/2
	cerrar3.Gravity=Gravity.CENTER
	
	
	
End Sub
Sub gimbal_Click()
	deshabilitar
	
	panel_gimbal.Initialize("")
	Activity.addview(panel_gimbal,0%x, 0%y, 100%x, 100%y)
	Dim titulo As Label
	titulo.Initialize("")
	titulo.text= "Gimbal Settings"
	titulo.TextSize=18
	titulo.gravity=Gravity.CENTER
	panel_gimbal.AddView(titulo, 50%x,0%y, 100%x,10%y)
	titulo.Left=50%x-titulo.Width/2
	panel_gimbal.Color=Colors.ARGB(255,0,0,0)
	For i=0 To 8
		labeltext(i).Initialize("")
		labelvalue(i+20).Initialize("labelvalue")
		labeltext(i).Text=set_label.Get(i+20)
		labelvalue(i+20).Text=sets(i+20)
		labelvalue(i+20).Tag=set_label.get(i+20)
		labelvalue(i+20).Color=Colors.White
		labelvalue(i+20).TextColor=Colors.Black
		panel_gimbal.AddView(labeltext(i),5%x, 10%y+i*8%y,50%x,40dip)
		panel_gimbal.AddView(labelvalue(i+20),10%x+labeltext(i).Width, 10%y+i*8%y,50dip,30dip)
		labelvalue(i+20).Gravity=Gravity.CENTER
		labeltext(i).Gravity=Gravity.CENTER
	Next 
	Dim cerrar4 As Button
	cerrar4.Initialize("cerrar4")
	cerrar4.Text="Close"
	panel_gimbal.AddView(cerrar4,50%x,90%y,100dip,40dip)
	cerrar4.Left=50%x-cerrar4.Width/2
	cerrar4.Gravity=Gravity.CENTER
	
End Sub
Sub misc_Click()
	deshabilitar
	
	panel_misc.Initialize("")
	Activity.addview(panel_misc,0%x, 0%y, 100%x, 100%y)
	Dim titulo As Label
	titulo.Initialize("")
	titulo.text= "Misc Settings"
	titulo.TextSize=18
	titulo.gravity=Gravity.CENTER
	panel_misc.AddView(titulo, 50%x,0%y, 100%x,10%y)
	titulo.Left=50%x-titulo.Width/2
	panel_misc.Color=Colors.ARGB(255,0,0,0)
	For i=0 To 1
		labeltext(i).Initialize("")
		labeltext(i).Text=set_label.Get(i+8)
		labeltext(i).Gravity=Gravity.CENTER
		labelvalue(i+8).Initialize("labelvalue")
		labelvalue(i+8).Text=sets(i+8)
		labelvalue(i+8).Tag=set_label.get(i+8)
		labelvalue(i+8).Color=Colors.White
		labelvalue(i+8).TextColor=Colors.Black
		labelvalue(i+8).Gravity=Gravity.CENTER
		panel_misc.AddView(labeltext(i),5%x, 10%y+i*10%y,50%x,40dip)
		panel_misc.AddView(labelvalue(i+8),10%x+labeltext(i).Width, 10%y+i*10%y,50dip,30dip)
		
		
	Next
	
	yawdir.Initialize("spektrum")
	yawdir.TextOff="0"
	yawdir.TextOn="1"
	If sets(10)=1 Then 
		yawdir.Checked=True
	Else 
		yawdir.Checked=False
	End If 
	labeltext(2).Initialize("")
	labeltext(2).Text=set_label.Get(10)
	labeltext(2).Gravity=Gravity.CENTER
	panel_misc.AddView(labeltext(2),5%x, 10%y+2*10%y,50%x,40dip)
	panel_misc.AddView(yawdir,10%x+labeltext(2).Width, 30%y,50dip,50dip)
	
	Dim cerrar5 As Button
	cerrar5.Initialize("cerrar5")
	cerrar5.Text="Close"
	panel_misc.AddView(cerrar5,50%x,90%y,100dip,40dip)
	cerrar5.Left=50%x-cerrar5.Width/2
	cerrar5.Gravity=Gravity.CENTER
	
End Sub
Sub baud_Click()
	deshabilitar
	panel_baud.Initialize("")
	Activity.addview(panel_baud,0%x, 0%y, 100%x, 100%y)
	Dim titulo As Label
	Dim spinners(2) As Spinner
	spinners(0).Initialize("spinners")
	spinners(1).Initialize("spinners")
	spinners(0).AddAll(Array As Int(1200,9600,14400,19200,38400,56000,57600,115200))
	spinners(1).AddAll(Array As Int(1200,9600,14400,19200,38400,56000,57600,115200))
	titulo.Initialize("")
	titulo.text= "Baud Settings"
	titulo.TextSize=18
	titulo.gravity=Gravity.CENTER
	panel_baud.AddView(titulo, 50%x,0%y, 100%x,10%y)
	titulo.Left=50%x-titulo.Width/2
	panel_baud.Color=Colors.ARGB(255,0,0,0)
	For i=0 To 1
		labeltext(i).Initialize("")
		labeltext(i).Text=set_label.Get(i+30)
		labeltext(i).Gravity=Gravity.CENTER
		
		For j=0 To spinners(i).Size-1
			If spinners(i).GetItem(j)=sets(30+i) Then 
				spinners(i).SelectedIndex=j
			End If 
		Next 
		spinners(i).Tag=set_label.get(i+30)
		
		panel_baud.AddView(labeltext(i),5%x, 10%y+i*10%y,30%x,40dip)
		panel_baud.AddView(spinners(i),10%x+labeltext(i).Width, 10%y+i*10%y,40%x,40dip)
	Next
	
	Dim cerrar6 As Button
	cerrar6.Initialize("cerrar6")
	cerrar6.Text="Close"
	panel_baud.AddView(cerrar6,50%x,90%y,100dip,40dip)
	cerrar6.Left=50%x-cerrar6.Width/2
	cerrar6.Gravity=Gravity.CENTER
	
End Sub


Sub habilitar
	rcsettings.Enabled=True
	vbat.Enabled=True	
	tri.Enabled=True
	gimbal.Enabled=True
	Misc.Enabled=True
	read_again.Enabled=True
	Set_Features.Enabled=True
	Baud.Enabled=True
End Sub
Sub deshabilitar
	rcsettings.Enabled=False
	vbat.Enabled=False	
	tri.Enabled=False
	gimbal.Enabled=False
	Misc.Enabled=False
	read_again.Enabled=False
	Set_Features.Enabled=False
	Baud.Enabled=False
End Sub

Sub cerrar_Click
	panel_rc_settings.RemoveView
	habilitar
End Sub
Sub cerrar2_Click
	panel_vbat.RemoveView
	habilitar
End Sub
Sub cerrar3_Click
	panel_tri.RemoveView
	habilitar
End Sub
Sub cerrar4_Click
	panel_gimbal.RemoveView
	habilitar
End Sub
Sub cerrar5_Click
	panel_misc.RemoveView
	habilitar
End Sub
Sub cerrar6_Click
	panel_baud.RemoveView
	habilitar
End Sub
Sub Spinners_ItemClick (Position As Int, Value As Object)
	Dim who As Spinner
	who=Sender
	sets(set_label.IndexOf(who.Tag))=who.SelectedItem
	changed(set_label.IndexOf(who.Tag))=True
End Sub
Sub data
If Main.connected Then 
		If cli_connected=False Then 
				timer2.Enabled=True
				Main.TextWriter1.Write("#")
				Main.TextWriter1.Flush
				cli_connected=True
				send_what="start"
				Dim Ti As Long
  				Ti = DateTime.Now + (200)
  				Do While DateTime.Now < Ti
     		 		DoEvents
   				Loop
				Timer2_Tick
				Main.TextWriter1.Write("feature" & CRLF)
				send_what="feature"
				Main.TextWriter1.Flush
				Ti = DateTime.Now + (400)
  				Do While DateTime.Now < Ti
     		 		DoEvents
   				Loop
				Timer2_Tick
				Main.Textwriter1.Write("set" & CRLF)
				send_what="set"
				Main.TextWriter1.Flush
				Timer2_Tick
				
		Else 
				Main.TextWriter1.Write("feature" & CRLF)
				send_what="feature"
				Main.TextWriter1.Flush
				Ti = DateTime.Now + (400)
  				Do While DateTime.Now < Ti
     		 		DoEvents
   				Loop
				Timer2_Tick
				Main.Textwriter1.Write("set" & CRLF)
				send_what="set"
				Main.TextWriter1.Flush
				Timer2_Tick
				
		End If 'cli_connected
		
End If 'connected
End Sub

Sub labelvalue_click
	Dim who As Label
	who=Sender
	popup(who.text,who.Tag)
	changed(set_label.IndexOf(who.Tag))=True
End Sub

'Sub deadband_Click
'	popup(settings(0).Text,"deadband")
'	changed(0)=True
'End Sub 
'Sub yawdeadband_Click
'	popup(settings(1).Text,"yawdeadband")
'	changed(1)=True
'End Sub 

Sub popup(inicial As Int, quien As String)
	Dim who As Label
	who=Sender
	
	Dim pnl As Panel
	pnl.Initialize("pnl")
	Dim bgnd As ColorDrawable
	bgnd.Initialize(Colors.Black, 5dip)
	pnl.Background = bgnd
	'Dialogo
	'seekbar
	lbl2.Initialize("lbl2")
	sk1.Initialize("sk1")
	valor_minimo=Mins(set_label.IndexOf(quien))
	valor_maximo=Maxs(set_label.IndexOf(quien))
	If Mins(set_label.IndexOf(quien))=0 Then 
		sk1.Max=Maxs(set_label.IndexOf(quien))
		valor_maximo=sk1.Max
	Else
		sk1.Max=valor_maximo-valor_minimo
		
	End If 	
	sk1.Value=inicial
	lbl2.Text=inicial
	
	pnl.AddView(sk1,5%x,10%y,70%x,25dip)
	lbl2.Gravity=Gravity.CENTER_HORIZONTAL
	pnl.AddView(lbl2,20%x,0%y,20%x,30dip)

	cd.AddView(pnl, 0%x, 0%y, 80%x, 100dip) ' sizing relative to the screen size is probably best
	If quien<>"yaw_direction" Then 
		a= pnl.Width/2-lbl2.Width/2
		lbl2.Left=a
	End If 
	
	ret = cd.Show("", "Ok", "Cancel", "", Null)
	If ret=DialogResponse.POSITIVE Then
		labelvalue(set_label.IndexOf(who.Tag)).Text=valor
		sets(set_label.IndexOf(who.Tag))=valor
			
	End If 
	
End Sub
Sub sk1_ValueChanged (Value As Int, UserChanged As Boolean)
	If valor_minimo=0 Then 
		valor=sk1.Value
		lbl2.Text=sk1.Value
	Else 
		valor=valor_minimo+sk1.Value
		lbl2.Text=valor_minimo+sk1.Value
	End If 
End Sub
Sub direccion_click
	If yawdir.Checked Then 
		valor=1
	Else 
		valor =-1
	End If 
End Sub 
Sub specktrum_click
	If yawdir.Checked Then 
		valor=1
	Else 
		valor =0
	End If 
End Sub 

Sub Set_Features_Click
	If Main.connected Then 
		For i= 0 To 9
			If checkboxes(i).Checked Then
				Dim text As String
				text="feature " & features(i) & CRLF
				Main.TextWriter1.Write(text)
				Main.TextWriter1.Flush
			Else
				Dim text As String
				text="feature -" & features(i) & CRLF
				Main.TextWriter1.Write(text)
				Main.TextWriter1.Flush
			End If
		Next
		For i=0 To changed.Length-1
			If changed(i) Then 
				Dim text,seteo,temp As String
				temp=""
				seteo= set_label.get(i)
				For k=0 To seteo.Length-1
					If seteo.CharAt(k) <> " " Then
						temp=temp & seteo.CharAt(k)
					Else 
					End If 
				Next
				text="set " & temp & " = " &  sets(i) & CRLF
				Main.TextWriter1.Write(text)
				Main.TextWriter1.Flush
			End If 
			
		Next
		Dim junk(1000) As Char
		Main.TextReader1.read(junk,0,1000)
		
		
	End If 
	
	
End Sub

Sub close_click()
	Activity_Pause(True)
End Sub

Sub Timer2_Tick
	If Main.TextReader1.Ready Then 'check if there is any data waiting to be read
			Dim output As String
			'Dim buff(1) As Char
			
			Select send_what
				Case "start"
					Dim buff(100) As Char
					cuantos=Main.TextReader1.read(buff,0,buff.length)
					output=CharsToString(buff,0,buff.Length)
				Case "feature"
					Dim buff(250) As Char
					cuantos= cuantos + Main.TextReader1.read(buff,0,buff.length)
					output=CharsToString(buff,0,buff.Length)
					For i=0 To 9
						If output.Contains(features(i))=True Then
							checkboxes(i).Checked=True
						End If
					Next
				Case "set"
					Dim buff(1000) As Char
					c= Main.TextReader1.read(buff,0,buff.length)
					output=CharsToString(buff,0,buff.Length)
					Dim value As Int
					Dim hasta As Int
					If output.Contains("Current settings:") Then 
						
						For m=0 To set_label.Size-1
							Dim B As Int
							Dim test As String
							test=set_label.Get(m)
							test=test.ToLowerCase
							test=test.Replace(" ","")
							B=output.IndexOf(test)
							If  B <> -1 Then 
								For i= B + test.Length To B + test.Length + 10
									If buff(i)="=" Then
										For h=1 To 10
											If buff(i+h)=Chr(10) Then 
												hasta=h
											End If 
										Next
										Try 
											sets(m)=CharsToString(buff,i+1, hasta)
										Catch
											Main.TextWriter1.Write("set" & CRLF)
											Main.TextWriter1.Flush
										End Try
										
									End If 
								Next
							Else 
								sets(m)= -1000
							End If
							
						Next 
					Else 
						Main.TextWriter1.Write("set" & CRLF)
						Main.TextWriter1.Flush
					End If 
					
'					For i=0 To labelvalue.Length-1
'						labelvalue(i).Text=sets(i)
'					Next
'					
			End Select
			
			
			'txtLog.SelectionStart = txtLog.Text.Length
		End If
End Sub
Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
	If Main.connected=True Then 
		Set_Features_Click
		Main.TextWriter1.Write("exit" & CRLF)
		Main.TextWriter1.Flush
		Dim junk(1000) As Char
		Main.TextReader1.read(junk,0,junk.length)
		' Dim Ti As Long
		' Ti = DateTime.Now + (1500)
		' 	Do While DateTime.Now < Ti
		'    	 DoEvents
		'   	Loop
		Main.return_from_cli=True
	End If 
	StartActivity(Main)
	Activity.Finish
End Sub

